﻿using MaiPlanServer.Dto;
using MaiPlanServer.IRepository;
using MaiPlanServer.IServices.IMasterDataServices;
using MaiPlanServer.Model;
using System;
using System.Collections.Generic;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace MaiPlanServer.Services.MasterDataServices
{
    public class MaterialProductsManageServicesL : IMaterialProductsManageServices
    {
        public MaterialProductsManageServicesL(IBaseRepository BaseConn)
        {
            this.BaseConn = BaseConn;
        }

        public IBaseRepository BaseConn { get; }

        public Task<bool> AddAsync(MD_ITEM model)
        {
            throw new NotImplementedException();
        }

        public Task<bool> DelAsync(string Id)
        {
            throw new NotImplementedException();
        }

        public async Task<int> GetCountAsync(string mdName, string mdCode, string groupName)
        {
            string sql = @"select count(1) from MD_ITEM mi inner join MD_ITEM_TYPE miy on miy.ITEM_TYPE_ID = mi.ITEM_TYPE_ID where 1=1 ";
            if (!string.IsNullOrEmpty(mdName))
            {
                sql += " and ITEM_NAME like @mdName ";
            }
            if (!string.IsNullOrEmpty(mdCode))
            {
                sql += " and ITEM_CODE like @mdCode ";
            }
            if (!string.IsNullOrEmpty(groupName))
            {
                sql += " and miy.ITEM_TYPE_NAME = @groupName ";
            }

            var result = await BaseConn.GetRecoreCountAsync(sql, new { mdName = "%" + mdName + "%", mdCode, groupName });
            return result;
        }

        public async Task<IEnumerable<MD_ITEMDto>> GetJsonAsync(int PageIndex, int PageSize, string mdName, string mdCode, string groupedName)
        {
            string sql = @"select mi.*,mu.MEASURE_ID,mu.MEASURE_NAME,miy.ITEM_TYPE_ID,miy.ITEM_TYPE_NAME from MD_ITEM mi inner join MD_UNIT_MEASURE mu on mi.UNIT_OF_MEASURE = mu.MEASURE_ID 
                            inner join MD_ITEM_TYPE miy on miy.ITEM_TYPE_ID = mi.ITEM_TYPE_ID where 1=1 ";
            if (!string.IsNullOrEmpty(mdName))
            {
                sql += " and mu.ITEM_NAME like @mdName ";
            }
            if (!string.IsNullOrEmpty(mdCode))
            {
                sql += " and mu.ITEM_CODE like @mdCode ";
            }
            if (!string.IsNullOrEmpty(groupedName))
            {
                sql += " and miy.ITEM_TYPE_NAME = @groupedName ";
            }
            sql += " order by Create_Time desc offset @PageSize*(@PageIndex-1) rows fetch next @PageSize row only";

            var list = await BaseConn.QueryAsync<MD_ITEMDto>(sql, new { PageIndex, PageSize, mdName = "%" + mdName + "%", mdCode, groupedName });

            return list;
        }

        public async Task<IEnumerable<MD_UNIT_MEASURE>> GetUnitJsonAsync()
        {
            string sql = @"select * from MD_UNIT_MEASURE";

            var list = await BaseConn.QueryAllAsync<MD_UNIT_MEASURE>(sql);
            return list;
        }

        public Task<bool> UpdateAsync(MD_ITEM model)
        {
            throw new NotImplementedException();
        }
    }
}
